//
//  NewMediaPromotionViewController.m
//  TryShopping
//
//  Created by LiYinDi on 16/6/13.
//  Copyright © 2016年 RuiBen. All rights reserved.
//

#import "NewMediaPromotionViewController.h"
#include "NewMediaPopView.h"
#import "CouponListViewController.h"
#import "ActivityViewController.h"
#import "DiscoverHouseholdListViewController.h"
#import "ExhibitionViewController.h"
#import "NewMediaPromotionCell.h"
#import "NewMediaPromotionModel.h"
#import "TheLoginViewController.h"
#import "MediaPromotionAddViewController.h"
#import "NewMediaDetailViewController.h"
#import "HouseholdDetailViewController.h"
#import "ActivityDetailViewController.h"

@interface NewMediaPromotionViewController ()<NewMediaPopViewDelegate>
{
    NSMutableArray *modelArray;
    NSUInteger _PageIndex;
    NSUInteger _TotalCount;
    NSUInteger _TotalPage;
      UIView*noDataView;
}
@end

@implementation NewMediaPromotionViewController
-(void)viewWillAppear:(BOOL)animated
{
    [[self rdv_tabBarController] setTabBarHidden:YES];
    self.navigationController.navigationBarHidden=YES;
    [self setAutoLayout];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    modelArray=[NSMutableArray array];
    _PageIndex=0;
    [self loadItemNextPage:_PageIndex];
}
-(void) loadItemNextPage:(NSUInteger)oldPageNum {
    if(oldPageNum!=0 && _TotalCount<=modelArray.count)
        return;
    if(oldPageNum == 0){
        [modelArray removeAllObjects];
        [_listTableView reloadData];
    }
    _PageIndex=oldPageNum+1;
    [self requestItemList: _PageIndex pageSize:6];
}
-(void)requestItemList:(NSUInteger)pageIndex pageSize:(NSUInteger)pageSize
{
    [noDataView removeFromSuperview];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    [Global showIndeterminateHud:@"加载中，请稍后。。。" delay:30 view:self.view];
    NSMutableDictionary *params=[[NSMutableDictionary alloc] init];
    [params setObject:Token forKey:@"tokenId"];
    NSNumber *pageSizeNumber = [NSNumber numberWithInteger:pageSize];
    NSNumber *pageIndexNumber = [NSNumber numberWithInteger:pageIndex];
    [params setObject:pageSizeNumber forKey:@"PageSize"];
    [params setObject:pageIndexNumber forKey:@"PageIndex"];
    [HttpRequest requestData:@"MediaPromotion/List" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
                _TotalCount=[[severDataDict.dataDict objectForKey:@"TotalCount"] integerValue];
//                NSLog(@"******===%@",severDataDict.dataDict);
               if (_TotalCount%6!=0) {
                    _TotalPage=_TotalCount/6+1;
                }
                else if (_TotalCount%6==0)
                {
                    _TotalPage=_TotalCount/6;
                }
                NSMutableArray * mItemsArray=[NSMutableArray arrayWithArray:[severDataDict.dataDict objectForKey:@"DataLine"]];
            if (mItemsArray.count>0) {
                for (NSDictionary *dic in mItemsArray) {
                    NewMediaPromotionModel *dataModel=[NewMediaPromotionModel dataModelWithDict:dic];
                    [modelArray addObject:dataModel];
                }
                
            }else{
                noDataView=[[UIView alloc]initWithFrame:CGRectMake(0, ScreenHeight/2-50, ScreenWidth, 80)];
                [self.view addSubview:noDataView];
                UIImageView*noImage=[[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth/2-47, 0, 94, 51)];
                noImage.image=[UIImage imageNamed:@"meiyoushuju"];
                [noDataView addSubview:noImage];
                UILabel*noLabel=[[UILabel alloc]initWithFrame:CGRectMake(0,65, ScreenWidth, 14)];
                noLabel.text=@"没有列表数据，赶紧去添加！";
                noLabel.textColor=zhonghuiColor; noLabel.font=[UIFont systemFontOfSize:14];
                noLabel.textAlignment=NSTextAlignmentCenter;
                [noDataView addSubview:noLabel];
                
            }

                [_listTableView reloadData];
                }
            else
            {
                [Global hideProgressHUDInternal:self.view];
                NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                [Global showTextHudSub:requestResultStr view:self.view];
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==9999) {
                    TheLoginViewController *loginVC=[[TheLoginViewController alloc] init];//2.0{9个参数 少2个}
                    [self.navigationController pushViewController:loginVC animated:YES];
                }
                
            }
        }
        else
        {
            [Global hideProgressHUDInternal:self.view];
            [Global showTextHudSub:severDataDict.resultStr view:self.view];
        }
    }];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectio
{
    return modelArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * const  ResultTableViewCellIdentifier = @"NewMediaPromotionCellID";
    NewMediaPromotionCell *cell=( NewMediaPromotionCell *)[tableView dequeueReusableCellWithIdentifier: ResultTableViewCellIdentifier];
    if (cell==nil) {
        NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"NewMediaPromotionCell" owner:self options:nil];
        cell = [array objectAtIndex:0];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model=modelArray[indexPath.row];
//    cell.typeLabel.text=@"应销活动";
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewMediaPromotionModel *Model=modelArray[indexPath.row];
    if ([Model.typeCode isEqualToString:@"scene"]) {
        [Global hideProgressHUDInternal:self.view];
        [Global showTextHudSub:@"h5界面在维护！即将上线" view:self.view];
//      NewMediaDetailViewController *detailVC=[[NewMediaDetailViewController alloc] init];
//      [self.navigationController pushViewController:detailVC animated:YES];
    }else if ([Model.typeCode isEqualToString:@"household"]){
    HouseholdDetailViewController *detailVC=[[HouseholdDetailViewController alloc] init];
    detailVC.fid=Model.shareId;//资讯分享的id就是他本身一开始的id
    [self.navigationController pushViewController:detailVC animated:YES];
    }else if ([Model.typeCode isEqualToString:@"act"]){
        ActivityDetailViewController *detailVC=[[ActivityDetailViewController alloc] init];
        detailVC.actId=Model.shareId;//活动分享的id就是他本身一开始的id
        [self.navigationController pushViewController:detailVC animated:YES];
    }
}
-(IBAction)addBtnClick:(id)sender
{
    [self customButtonClick];
    
//    NewMediaPopView *popView=[[NewMediaPopView alloc] init];
//    popView.delegate=self;
//    [popView show];
}
#pragma mark NewMediaPopViewDelegate
-(void)couponButtonClick
{
    CouponListViewController *couponVC=[[CouponListViewController alloc] init];
    [self.navigationController pushViewController:couponVC animated:YES];
}
-(void)marketingActivityButtonClick
{
    
    ActivityViewController *actVC=[[ActivityViewController alloc] init];
    [self.navigationController pushViewController:actVC animated:YES];
}
-(void)informationButtonClick
{
    DiscoverHouseholdListViewController *disVC=[[DiscoverHouseholdListViewController alloc] init];
    disVC.isHidden=YES;
    [self.navigationController pushViewController:disVC animated:YES];
//     [[self rdv_tabBarController] setSelectedIndex:3];
}
-(void)scenesButtonClick
{
    ExhibitionViewController *exhiVC=[[ExhibitionViewController alloc] init];
    exhiVC.titleStr=@"场景";
    exhiVC.isHidden=YES;
    [self.navigationController pushViewController:exhiVC animated:YES];
    
//    [[self rdv_tabBarController] setSelectedIndex:1];
}
-(void)customButtonClick
{
    MediaPromotionAddViewController *exhiVC=[[MediaPromotionAddViewController alloc] init];
//    exhiVC.titleStr=@"场景";
//    exhiVC.isHidden=YES;
    [self.navigationController pushViewController:exhiVC animated:YES];

}
-(void)setAutoLayout
{
    [_topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.top.equalTo(self.view.mas_top);
        make.height.equalTo(@64);
    }];
    [_topLineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_topView.mas_left);
        make.right.equalTo(_topView.mas_right);
        make.bottom.equalTo(_topView.mas_bottom).with.offset(-1);
        make.height.equalTo(@0.5);
    }];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(_topView.mas_centerX);
        make.height.equalTo(@30);
        make.width.equalTo(@110);
        make.top.equalTo(_topView.mas_top).with.offset(25);
    }];
    
    [_backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_titleLabel.mas_centerY);
        make.height.equalTo(@30);
        make.width.equalTo(@60);
        make.left.equalTo(_topView.mas_left);
    }];
    [_addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_titleLabel.mas_centerY);
        make.height.equalTo(@30);
        make.width.equalTo(@42);
        make.right.equalTo(self.view.mas_right).with.offset(-5);
    }];
    [_listTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.top.equalTo(_topView.mas_bottom).with.offset(-20);
        make.bottom.equalTo(self.view.mas_bottom);
    }];
}
-(IBAction)BackAction:(id)sender
{
     [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
}
@end
